package main

import (
	"fmt"
	"reflect"
	"teamJDI/database"
	"teamJDI/task"
	"teamJDI/util"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
)

func ValidateJSONDateType(field reflect.Value) interface{} {
	if field.Type() == reflect.TypeOf(util.LocalTime{}) {
		timeStr := field.Interface().(util.LocalTime).String()
		// 0001-01-01 00:00:00 是 go 中 time.Time 类型的空值
		// 这里返回 Nil 则会被 validator 判定为空值，而无法通过 `binding:"required"` 规则
		if timeStr == "0001-01-01 00:00:00" {
			return nil
		}
		return timeStr
	}
	return nil
}
func main() {
	database.InitMySQL()
	defer database.DB.Close()
	r := gin.Default()
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		// 注册 util.LocalTime 类型的自定义校验规则
		v.RegisterCustomTypeFunc(ValidateJSONDateType, util.LocalTime{})
	}
	task.Routers(r)
	// listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
	if err := r.Run(); err != nil {
		fmt.Println("startup service failed, err:%v\n", err)
	}
}
